package com.fenghongzhang.music.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import com.fenghongzhang.music.entity.Music;

import java.util.ArrayList;
import java.util.List;

public class MusicUtils {

    private static final String TAG = "MusicUtils";
    public static List<Music> getMusics(Context context){
        List<Music> musics = new ArrayList<>();


        //从sd卡中读取

        ContentResolver resolver = context.getContentResolver();

        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

        if(cursor != null){
            while(cursor.moveToNext()){
                Music music = new Music();
                music.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
                music.setArtist(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                music.setDuration(duration);
                music.setData(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));

                //读取音乐的专辑封面信息
                int anInt = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
//                Log.i(TAG, "getMusics: "+anInt);
                String album = getAlbum(context, anInt); //封面的地址
//                Log.i(TAG, "getMusics: "+album);

                music.setImgPath(album);
                //过滤短音频
                if(Integer.parseInt(duration) > 1000 * 100){
                    musics.add(music);
                }

            }

            cursor.close();
        }else{
            Toast.makeText(context, "没有音乐文件", Toast.LENGTH_SHORT).show();

        }

        return musics;
    }

        //取到专辑的图片
        public static String getAlbum(Context context,int id){
            String mUriAlbums = "content://media/external/audio/albums";
            String[] projection = new String[]{"album_art"};
            ContentResolver contentResolver = context.getContentResolver();
            Cursor query = contentResolver.query(Uri.parse(mUriAlbums + "/" + id), projection, null, null, null);
            if(query != null){
                //过滤一下短音频
                if (query.getCount() > 0 && query.getColumnCount() > 0) {
                    query.moveToNext();
                    String string = query.getString(0);
                    Log.i(TAG, "getAlbum: "+string);
                    return string;
                }
                query.close();
            }

            return null;

        }

    //格式化时间
    public static String formatTime(int time){
        if(time / 1000 % 60 < 10){
            //4:09
            return time / 1000 / 60 +":0"+ time / 1000 % 60;
        }else{
            //5:30
            return time / 1000 / 60 +":"+ time / 1000 % 60;
        }

    }

}
